#include "Syscall.h"
#include "LibSyscall.h"
#include "Const.h"
#include "Task.h"
#include "Debug.h"
#include "utility/Types.h"

typedef int (*SyscallHandler)(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3);


static const SyscallHandler syscallHandlers[] = {
    [SYS_PRINT_MSG] = (SyscallHandler)SysPrintMsg,
    [SYS_SLEEP] = (SyscallHandler)SysSleep,
    [SYS_GET_PID] = (SyscallHandler)SysGetPid,
    [SYS_FORK] = (SyscallHandler)SysFork,
    [SYS_EXECVE] = (SyscallHandler)SysExecve,
    [SYS_YIELD] = (SyscallHandler)SysYield
};


void HandleSyscall(SyscallFrameType* frame)
{
    // int func_id = frame->eax;
    // int arg0 = frame->ebx;
    // int arg1 = frame->ecx;
    // int arg2 = frame->edx;
    // int arg3 = frame->esi;

    if (frame->func_id < sizeof(syscallHandlers) / sizeof(syscallHandlers[0]))
    {
        SyscallHandler handler = syscallHandlers[frame->func_id];
        if (handler)
        {
            int ret = handler(frame->arg0, frame->arg1, frame->arg2, frame->arg3);
            frame->eax = ret;
            return;
        }
    }

    frame->eax = -1;
}